package com.powerteam.controller.crm;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.crm.Customer;
import com.powerteam.model.crm.CustomerCategory;
import com.powerteam.model.crm.Industry;
import com.powerteam.model.crm.Source;
import com.powerteam.service.crm.CustomerService;
import com.powerteam.vo.Result;
import com.powerteam.vo.crm.QueryCustomerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 客户页面相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/customer")
public class CustomerController extends AuthorizedController {

    @Autowired
    private CustomerService customerService;

    /**
     * 客户管理页
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String customer() {

        return "crm/customer";
    }

    /**
     * 分页查询客户信息
     *
     * @param vo
     * @return PageInfo<Customer>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public PageInfo<Customer> find(@RequestBody QueryCustomerVO vo) {

        return customerService.find(vo);
    }

    /**
     * 查找所有客户类别
     *
     * @param
     * @return List<CustomerCategory>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/findAllCustomerCategory", method = RequestMethod.POST)
    public List<CustomerCategory> findAllCustomerCategory() {

        return customerService.findAllCustomerCategory();
    }

    /**
     * 查找所有行业
     *
     * @param
     * @return List<Industry>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/findAllIndustry", method = RequestMethod.POST)
    public List<Industry> findAllIndustry() {

        return customerService.findAllIndustry();
    }

    /**
     * 查找所有来源
     *
     * @param
     * @return List<Source>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/findAllSource", method = RequestMethod.POST)
    public List<Source> findAllSource() {

        return customerService.findAllSource();
    }

    /**
     * 添加客户
     *
     * @param customer
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Customer customer) {

        customer.setCreateBy(super.getUser().getUserId());
        return customerService.insert(customer);
    }

    /**
     * 查询客户是否存在
     *
     * @param customer
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/checkCustomerName", method = RequestMethod.POST)
    public Result checkCustomerName(@RequestBody Customer customer) {

        return customerService.checkCustomerName(customer);
    }

    /**
     * 根据Id查询客户信息
     *
     * @param customer
     * @return Customer
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Customer findById(@RequestBody Customer customer) {

        return customerService.findById(customer.getCustomerId());
    }

    /**
     * 修改客户信息
     *
     * @param customer
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Customer customer) {

        return customerService.update(customer);
    }

    /**
     * 客户看板
     *
     * @param customerId
     * @return ModelAndView
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @RequestMapping(value = "/dashboard/{customerId}", method = RequestMethod.GET)
    public ModelAndView dashboard(@PathVariable int customerId) {

        ModelAndView vm = new ModelAndView("crm/customerDashboard");
        vm.addObject("customerId", customerId);
        return vm;
    }

    /**
     * 修改星级信息
     *
     * @param customer
     * @Author jinming.zhang
     * @Date 2022/1/10
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "/updateStar", method = RequestMethod.POST)
    public Result updateStar(@RequestBody Customer customer) {

        return customerService.updateStar(customer);
    }

    /**
     * 更新位置
     *
     * @param customer
     * @Author jinming.zhang
     * @Date 2022/1/10
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
    public Result updateLocation(@RequestBody Customer customer) {

        return customerService.updateLocation(customer);
    }

    /**
     * 删除客户
     *
     * @param customerId
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/4/13
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody Integer customerId) {

        return customerService.remove(customerId);
    }

}
